package apple.encryptedNode.activities;

import it.telecomitalia.jchat.R;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import apple.encryptedNode.Constans;
import apple.encryptedNode.dao.OperationDAO;
import apple.encryptedNode.view.NodeListView;
import apple.encryptedNode.vo.Node;
import apple.encryptedNote.tools.DesUtils;
import apple.encryptedNote.tools.SMSService;

/***
 * 秘密箱列表
 * **/
public class NodeListActivity extends Activity {
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	private String tag = NodeListActivity.class.getName();
	NodeListView listView;
	ArrayList<Node> list;
	Node bean;
	OperationDAO dao;
	 EditText passwordEditText;//密码输入框
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// TODO Put your code here
		Log.d(tag, "onCreate");
		setContentView(R.layout.nodelist);
		listView = (NodeListView) findViewById(R.id.nodeList);

		listView.setOnItemLongClickListener(listenter);
		list = listView.getList();
		dao = new OperationDAO(this);
	}

	OnItemLongClickListener listenter = new OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int postion, long arg3) {
			// TODO Auto-generated method stub
			bean = list.get(postion);
			Log.d(tag, "onItemLongClick");
			final CharSequence[] items = { getString(R.string.open),
					getString(R.string.delete), getString(R.string.encrypt),
					getString(R.string.decrypt), getString(R.string.send) };
			AlertDialog.Builder builder = new AlertDialog.Builder(
					NodeListActivity.this);
			builder.setTitle("操作选项");
			builder.setItems(items, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					menu(which);
				}

			});
			AlertDialog alert = builder.create();
			alert.show();
			return true;
		}

	};

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		listView.refresh();
	}

	/**
	 * 菜单操作
	 * **/
	public void menu(int menuid) {
		switch (menuid) {
		case 0:// open
			Log.d(tag, "open");
			open();

			break;
		case 1:// delete
			Log.d(tag, "delete");
			delete();
			break;
		case 2:// encrypt加密
			Log.d(tag, "encrypt");
			encrypt();
			break;

		case 3:// decrypt
			Log.d(tag, "send");
			decrypt();
			break;
		case 4:// send
			Log.d(tag, "send");
			send();
			break;
		}
	}

	/**
	 * 打开菜单
	 * **/ 
	private void open() 
	{
		if (bean.getEncryptionStatus().equals("n"))// 未加密：
		{
			Intent newNoteActivityIntent = new Intent();
			newNoteActivityIntent.putExtra("keyId", bean.getId());
			newNoteActivityIntent.putExtra("keyName", bean.getTopicName());
			newNoteActivityIntent.putExtra("keyContent", bean.getTopicContent());
			newNoteActivityIntent.setClass(NodeListActivity.this,
					NewNoteActivity.class);
			startActivity(newNoteActivityIntent);
		}else if (bean.getEncryptionStatus().equals("y"))// 加密状态：已加密
		{
			Toast.makeText(NodeListActivity.this, "已经加密，请先解密", Toast.LENGTH_LONG).show();
		}
	}

	/***
	 * 删除菜单操作
	 * **/ 
	private void delete() {
		if (bean.getEncryptionStatus().equals("n"))// 未加密：
		{
			dao.delete(bean.getId());
			listView.refresh();
		}
		else if (bean.getEncryptionStatus().equals("y"))// 加密状态：已加密
		{
			AlertDialog.Builder builder = new AlertDialog.Builder(
					NodeListActivity.this);
			builder.setMessage("请先解密!")
					.setCancelable(false)
					.setNegativeButton("Comfirm",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							});
			AlertDialog alert = builder.create();
			alert.show();

		}
	}
	 EditText pwdEdit;
	/***
	 * 
	 * 加密菜单操作
	 * **/ 
	private void encrypt() {
		Log.d(tag, "encrypt");
		Log.d(tag, (bean.getEncryptionStatus() == "n") + "");
		Log.d(tag, (bean.getEncryptionStatus() == "n") + "");
		if (bean.getEncryptionStatus().equals("n"))// 未加密
		{
			final Dialog encryptionDialog = new Dialog(NodeListActivity.this);
			encryptionDialog.setContentView(R.layout.encryptiondialog); //简单起见，只输入密码，进度维度去掉了先
			//encryptionDialog.setContentView(R.layout.encryptiondialog);
			  pwdEdit=(EditText)encryptionDialog.findViewById(R.id.EditText03);
			
			encryptionDialog.setTitle("请核对信息无误!");
			Button dialogOperateButton = (Button) encryptionDialog
					.findViewById(R.id.dialogOperateButton);
			dialogOperateButton.setText("加密");
			Button dialogCancelButton = (Button) encryptionDialog
					.findViewById(R.id.dialogCancelButton);
			 dialogCancelButton
			 .setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					encryptionDialog.cancel();
				}
			});
			 dialogOperateButton
			 .setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					try {
						String password=pwdEdit.getText().toString();
						if(password.equals(""))
						{
							Toast.makeText(NodeListActivity.this, "密码不能为空：", Toast.LENGTH_LONG).show();
						}else if(!password.equals(Constans.password))
						{
							Toast.makeText(NodeListActivity.this, "密码错误：", Toast.LENGTH_LONG).show();
						}else if(password.equals(Constans.password))
						{
							String res=DesUtils.encode(Constans.key, bean.getTopicContent());
							Log.d(tag, "加密结果："+res);
							dao.updateStatus(bean.getId(), "", res, "y");
							listView.refresh();
							Toast.makeText(NodeListActivity.this, "加密结果："+res, Toast.LENGTH_LONG).show();
							encryptionDialog.cancel();
						}
						
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						Toast.makeText(NodeListActivity.this, "加密失败", Toast.LENGTH_LONG).show();
					}
				}
			});

			encryptionDialog.show();
		} else if (bean.getEncryptionStatus().equals("y"))// 以加密
		{
			AlertDialog.Builder builder = new AlertDialog.Builder(
					NodeListActivity.this);
			builder.setMessage("已经加密!")
					.setCancelable(false)
					.setNegativeButton("Comfirm",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							});
			AlertDialog alert = builder.create();
			alert.show();
		}
	}

	/****
	 * 解密菜单操作
	 * */ 
	private void decrypt() {
		Log.d(tag, "decrypt");
		if (bean.getEncryptionStatus().equals("n"))// 未加密
		{
			AlertDialog.Builder builder = new AlertDialog.Builder(
					NodeListActivity.this);
			builder.setMessage("未加密文件，无需解密!")
					.setCancelable(false)
					.setNegativeButton("Comfirm",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							});
			AlertDialog alert = builder.create();
			alert.show();
		} else if (bean.getEncryptionStatus().equals("y"))// 以加密
		{
			final Dialog decryptionDialog = new Dialog(NodeListActivity.this);
			decryptionDialog.setContentView(R.layout.decryptiondialog);

			decryptionDialog.setTitle(" 请输入密码");

			Button dialogOperateButtonForDe = (Button) decryptionDialog
					.findViewById(R.id.dialogOperateButton2);

			dialogOperateButtonForDe.setText("解密");

			Button dialogCancelButtonForDe = (Button) decryptionDialog
					.findViewById(R.id.dialogCancelButton2);// 取消按钮
			
			passwordEditText=(EditText) decryptionDialog.findViewById(R.id.DeditText1);// 取消按钮
			
			
			dialogCancelButtonForDe.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					decryptionDialog.dismiss();
				}

			});
			dialogOperateButtonForDe.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Log.d(tag, "解密dialogOperateButtonForDe");
					String password=passwordEditText.getText().toString();
					if(password.equals(""))
					{
						Toast.makeText(NodeListActivity.this, "密码不能为空", Toast.LENGTH_LONG).show();
					}
					else if(!password.equals(Constans.password))
					{
						Toast.makeText(NodeListActivity.this, "密码输入错误", Toast.LENGTH_LONG).show();
					}
					else 
					{
						Log.d(tag, "开始解密");
						
						Log.d(tag, "bean.getEncryptfileContent()："+bean.getEncryptfileContent());

						String result=DesUtils.decode(Constans.key, bean.getEncryptfileContent());
						Log.d(tag, "解密结果："+result);
						Toast.makeText(NodeListActivity.this,result, Toast.LENGTH_LONG).show();
						dao.updateStatus(bean.getId(),  result,"", "n");
						listView.refresh();
						decryptionDialog.dismiss();
					}
				}

			});
			decryptionDialog.show();
		}
	}

	private void send() {
		if (bean.getEncryptionStatus().equals("n"))// 未加密
		{
			final Dialog encryptionDialog = new Dialog(NodeListActivity.this);
			encryptionDialog.setContentView(R.layout.encryptiondialog); //简单起见，只输入密码，进度维度去掉了先
			//encryptionDialog.setContentView(R.layout.encryptiondialog);
			  pwdEdit=(EditText)encryptionDialog.findViewById(R.id.EditText03);
			
			encryptionDialog.setTitle("未加密，请核对信息无误!");
			Button dialogOperateButton = (Button) encryptionDialog
					.findViewById(R.id.dialogOperateButton);
			dialogOperateButton.setText("加密");
			Button dialogCancelButton = (Button) encryptionDialog
					.findViewById(R.id.dialogCancelButton);
			 dialogCancelButton
			 .setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					encryptionDialog.cancel();
				}
			});
			 dialogOperateButton
			 .setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					try {
						String password=pwdEdit.getText().toString();
						if(password.equals(""))
						{
							Toast.makeText(NodeListActivity.this, "密码不能为空：", Toast.LENGTH_LONG).show();
						}else if(!password.equals(Constans.password))
						{
							Toast.makeText(NodeListActivity.this, "密码错误：", Toast.LENGTH_LONG).show();
						}else if(password.equals(Constans.password))
						{
							String res=DesUtils.encode(Constans.key, bean.getTopicContent());
							Log.d(tag, "加密结果："+res);
							dao.updateStatus(bean.getId(), "", res, "y");
							listView.refresh();
							
							Toast.makeText(NodeListActivity.this, "加密结果："+res, Toast.LENGTH_LONG).show();
							encryptionDialog.cancel();
						}
						
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						Toast.makeText(NodeListActivity.this, "加密失败", Toast.LENGTH_LONG).show();
					}
				}
			});

			encryptionDialog.show();
		}else if (bean.getEncryptionStatus().equals("y"))// 未加密
		{
			final EditText neiriong=new EditText(this);
			
			AlertDialog alt=	new AlertDialog.Builder(NodeListActivity.this).create();
			alt.setTitle("请输入电话号码");
			alt.setView(neiriong);
			
			alt.setButton("确定", new DialogInterface.OnClickListener()
			{

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					String phone=neiriong.getText().toString();
					if(phone.equals(""))
					{
						Toast.makeText(NodeListActivity.this, "不能為空", Toast.LENGTH_LONG).show();
					}else 
					{
						SMSService sms=new SMSService(NodeListActivity.this);
						sms.send(phone, bean.getEncryptfileContent());
					}
				}
			});
			alt.show();
		}
	}

}
